package com.woniu.his.mms.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.his.mms.app.consumer.dto.MedicineDto;
import com.woniu.his.mms.client.MedicineClient;
import com.woniu.his.mms.client.param.CreateMedicineParam;
import com.woniu.his.mms.client.result.DmsMedicineItemRecordResult;
import com.woniu.his.mms.client.result.MedicinePrescriptionReuslt;
import com.woniu.his.mms.client.result.MedicineResult;
import com.woniu.his.mms.common.MmsStatusEnum.MmsStatusCode;
import com.woniu.his.mms.infra.model.MedicineItemRecord;
import com.woniu.his.mms.infra.model.MedicinePrescriptionRecord;
import com.woniu.his.mms.service.MedicineItemRecordService;
import com.woniu.his.mms.service.MedicinePrescriptionRecordService;
import com.woniu.his.mms.service.dto.MedicinePrescriptionDto;
import com.woniu.his.mms.service.param.CreateMedicineServiceParam;
import com.woniu.his.result.CommonResultCode;
import com.woniu.his.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.checkerframework.checker.units.qual.C;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version: java version 1.8
 * @Author: Mr tao
 * @description:
 * @date: 2022-11-28 10:46
 */
@Slf4j
@RequestMapping("mms")
@RestController
public class MedicineImpl implements MedicineClient {

    @Resource
    private MedicinePrescriptionRecordService meservice;

    @Resource
    private MedicineItemRecordService itemRecordService;


    @Resource
    private RocketMQTemplate rmt;

    /**
     * 开立药方
     * @param param
     * @return
     */
    @Override
    public Result applyMedicine(CreateMedicineParam param) {
        log.info("开立药方:{}",param);
        //类型转换 参数校验
        CreateMedicineServiceParam param1 =  BeanUtil.copyProperties(param,CreateMedicineServiceParam.class);
        Long b=meservice.applyMedicine(param1);
        if(b>0){
            //设置一个延时消息 如果不去缴费就作废处方
            long delayTimes = 60 * 1000 * 60 * 12;
            MedicineDto medicineDto = new MedicineDto();
            medicineDto.setId(b);
            Message<MedicineDto> build = MessageBuilder.withPayload(medicineDto).build();
            rmt.syncSend("hospital-Medicine-apply",build,delayTimes,12);
            return Result.getInstance();
        }
        return Result.getInstance().setCode(CommonResultCode.GENERAL_FAIL.getCode()).setMsg("开立处方失败");
    }

    /**
     * 延时消息去查看用户是否已经付款
     * @param id
     * @return
     */
    @Override
    public Result invalid(Long id) {
        log.info("延时小时查询是否付款:{}",id);
        MedicinePrescriptionRecord byId = meservice.getById(id);
        if(byId.getMedicinePrescriptionStatus().equals("NOT_PAY")){
            //作废处方
            List<Long> ids = new ArrayList<>();
            ids.add(byId.getId());
            this.invalidMedicine(ids);
        }
        return Result.getInstance();
    }

    /**
     * 作废药方
     * @param ids
     * @return
     */
    @Override
    public Result invalidMedicine(List<Long> ids) {
        log.info("作废处方:{}",ids);
        boolean b=meservice.invalidMedicine(ids);
        return b?Result.getInstance():Result.getInstance().setCode(CommonResultCode.GENERAL_FAIL.getCode()).setMsg("作废失败");
    }

    /**
     * 根据病例id 查询药方
     * @param caseHistoryId
     * @return
     */
    @Override
    public Result<List<MedicineResult>> listByReg(Long caseHistoryId) {
        log.info("根据病例id 查询药方:{}",caseHistoryId);
        QueryWrapper<MedicinePrescriptionRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("case_history_id", caseHistoryId);
        List<MedicinePrescriptionRecord> list = meservice.list(wrapper);
        if(list.isEmpty())return null;
        List<MedicineResult> medicineResults = BeanUtil.copyToList(list, MedicineResult.class);
        for (MedicineResult medicineResult : medicineResults) {
            List<MedicineItemRecord> prescription_id = itemRecordService.list(new QueryWrapper<MedicineItemRecord>().eq("prescription_id", medicineResult.getId()));
            medicineResult.setDmsMedicineItemRecordList(BeanUtil.copyToList(prescription_id, DmsMedicineItemRecordResult.class));
        }
        return Result.getInstanceList(MedicineResult.class).setData(medicineResults);
    }

    /**
     * 根据药方id查询
     * @param ids
     * @return
     */
    @Override
    public Result<List<DmsMedicineItemRecordResult>> listByIds(List<Long> ids) {
        log.info("根据药方id 查询药方条目:{}",ids);
        QueryWrapper<MedicineItemRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("prescription_id", ids);
        List<MedicineItemRecord> list = itemRecordService.list(wrapper);
        return Result.getInstanceList(DmsMedicineItemRecordResult.class).setData(BeanUtil.copyToList(list,DmsMedicineItemRecordResult.class));
    }

    @Override
    public Result<MedicinePrescriptionReuslt> queryMedicinePrescriptionByBillId(Long BillId) {
        log.info("根据账单id查询药方: {}",BillId);
        MedicinePrescriptionDto medicinePrescriptionDto = meservice.queryMedicinePrescriptionByBillId(BillId);
        MedicinePrescriptionReuslt medicinePrescriptionReuslt = BeanUtil.copyProperties(medicinePrescriptionDto, MedicinePrescriptionReuslt.class);
        return Result.getInstance(MedicinePrescriptionReuslt.class).setData(medicinePrescriptionReuslt);
    }

    @Override
    public Result<Boolean> paySuccess(Long billId) {
        log.info("支付成功修改状态参数：{}",billId);
        Boolean bool = meservice.paySuccess(billId);
        return Result.getInstance(Boolean.class).setData(bool);
    }
}
